<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic" ],
            function(array, xhr, request, topic) {
                var deletItemsCount = 0;
                dutysectionRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#dutysectionGird").jqGrid('getGridParam', 'selrow');
                        var dutysection = jQuery("#dutysectionGird").jqGrid('getRowData', rowid);
                        dijit.byId('dutysectionUFD').setValues(dutysection);
                    }
                };
                topic.subscribe("dutysection/insertOrUpdate", dutysectionRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(
                            function(response) {
                                topic.publish("notifyMsgTopic", {
                                    message : response == '"success"' ? "成功" : "失败",
                                    type : "message",
                                    duration : 1000
                                });
                                var currentPage = jQuery('#dutysectionGird').jqGrid('getGridParam', 'page');
                                var pageSize = jQuery('#dutysectionGird').jqGrid('getGridParam', 'rowNum');
                                var totalCount = jQuery('#dutysectionGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                                var totalPages = Math.ceil(totalCount / pageSize);
                                jQuery("#dutysectionGird").jqGrid('setGridParam', {
                                    page : currentPage > totalPages ? totalPages : currentPage
                                }).trigger("reloadGrid");
                            }, function(error) {
                                alert(error);
                            });
                };
                dutysectionSubmitForm = function(formId, type) {
                    var url = "dutysection/insert";
                    if (type == 'u') {
                        url = "dutysection/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = dojo.toJson(form.getValues(), true);
                        syncDB(url, json);
                        form.hide();
                        topic.publish("dutysection/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                dutysectionDelete = function() {
                    var ids = jQuery("#dutysectionGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var dutysection = {};
                            dutysection.id = '';
                            array.forEach(ids, function(entry, i) {
                                dutysection.id += entry + ",";
                            });
                            syncDB("dutysection/delete", dojo.toJson(dutysection, true));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                dutysectionQuery = function(formId) {
                    //赋值给隐藏域
                    setHidenValue();
                    var dutysection = dijit.byId(formId).getValues();
                    jQuery("#dutysectionGird").jqGrid('setGridParam', {
                        postData : dutysection
                    }).trigger('reloadGrid');
                };
                //为隐藏域赋值
                var setHidenValue = function(){
                    var dutystarttime = dojo.byId("dutystarttime");
                    var startsignin = dojo.byId("startsignin");
                    var endsignin = dojo.byId("endsignin");
                    var dutyendtime = dojo.byId("dutyendtime");
                    var earlyofftime = dojo.byId("earlyofftime");
                    var signouttime = dojo.byId("signouttime");
                    
                    var _dutystarttime = dijit.byId("_dutystarttime");
                    var _startsignin = dijit.byId("_startsignin");
                    var _endsignin = dijit.byId("_endsignin");
                    var _dutyendtime = dijit.byId("_dutyendtime");
                    var _earlyofftime = dijit.byId("_earlyofftime");
                    var _signouttime = dijit.byId("_signouttime");
                    _dutystarttime.setValue(dutystarttime.value);
                    _startsignin.setValue(startsignin.value);
                    _endsignin.setValue(endsignin.value);
                    _dutyendtime.setValue(dutyendtime.value);
                    _earlyofftime.setValue(earlyofftime.value);
                    _signouttime.setValue(signouttime.value);
                };
            }
    );
</script>
<div data-dojo-type="dijit/Dialog" id="dutysectionIFD" title="新增">
	<div class="dijitDialogPaneContentArea">
		<table>
			<tr>
				<td><label for="sectionname">班次名称: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					data-dojo-props="required: true, trim:true" type="text"
					name="sectionname"></td>
			</tr>
			<tr>
				<td><label for="startsignin">开始签到: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					class="Wdate" data-dojo-props="required: true, trim:true"
					type="text" name="startsignin"
					onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>

			</tr>
			<tr>
				<td><label for="dutystarttime">上班时间: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					class="Wdate" data-dojo-props="required: true, trim:true"
					type="text" name="dutystarttime"
					onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
			<tr>
				<td><label for="endsignin">签到结束时间: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					class="Wdate" data-dojo-props="required: true, trim:true"
					type="text" name="endsignin"
					onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
			<tr>
				<td><label for="earlyofftime">早退计时: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					class="Wdate" data-dojo-props="required: true, trim:true"
					type="text" name="earlyofftime"
					onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
			<tr>
				<td><label for="dutyendtime">下班时间: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					class="Wdate" data-dojo-props="required: true, trim:true"
					type="text" name="dutyendtime"
					onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
			<tr>
				<td><label for="signouttime">签退结束: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					class="Wdate" data-dojo-props="required: true, trim:true"
					type="text" name="signouttime"
					onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
		</table>
	</div>
	<div class="dijitDialogPaneActionBar">
		<button data-dojo-type="dijit/form/Button" type="button"
			onclick="dutysectionSubmitForm('dutysectionIFD','i')">保存</button>
		<button data-dojo-type="dijit/form/Button" type="button"
			onclick="dutysectionRestForm('dutysectionIFD');">重置</button>
	</div>
</div>
<div data-dojo-type="dijit/Dialog" id="dutysectionUFD" title="更新">
	<div class="dijitDialogPaneContentArea">
		<input data-dojo-type="dijit/form/ValidationTextBox"
			data-dojo-props="required: true, readOnly: true" type="text"
			name="sectionid" style="display: none;">
		<table>
			<tr>
				<td><label for="sectionname">班次名称: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					data-dojo-props="required: true" type="text" name="sectionname"></td>
			</tr>
			<tr>
				<td><label for="startsignin">开始签到: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					data-dojo-props="required: true" type="text" name="startsignin"
					class="Wdate" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
			<tr>
				<td><label for="dutystarttime">上班时间: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					data-dojo-props="required: true" type="text" name="dutystarttime"
					class="Wdate" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
			<tr>
				<td><label for="endsignin">签到结束时间: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					data-dojo-props="required: true" type="text" name="endsignin"
					class="Wdate" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
			<tr>
				<td><label for="earlyofftime">早退计时: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					data-dojo-props="required: true" type="text" name="earlyofftime"
					class="Wdate" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
			<tr>
				<td><label for="dutyendtime">下班时间: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					data-dojo-props="required: true" type="text" name="dutyendtime"
					class="Wdate" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
			<tr>
				<td><label for="signouttime">签退结束: </label></td>
				<td><input data-dojo-type="dijit/form/ValidationTextBox"
					data-dojo-props="required: true" type="text" name="signouttime"
					class="Wdate" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
			</tr>
		</table>
	</div>
	<div class="dijitDialogPaneActionBar">
		<button data-dojo-type="dijit/form/Button" type="button"
			onclick="dutysectionSubmitForm('dutysectionUFD','u')">保存</button>
		<button data-dojo-type="dijit/form/Button" type="button"
			onclick="dutysectionRestForm('dutysectionUFD');">重置</button>
	</div>
</div>
<fieldset>
	<legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
	<form id="queryDutySectionForm" data-dojo-type="dijit/form/Form"
		method="post">
		<div style="margin-left: 5px;">
			<table border="0">
			     <tr>
                    <td><label for="sectionname">班次名称: </label></td>
                    <td colspan="5"><input type="text" name="sectionname" data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" style="width: 99.7%"></td>
                </tr>
				<tr>
					<td><label for="dutystarttime">上班时间: </label></td>
					<td><input id="dutystarttime" type="text" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
					<td><label for="startsignin">开始签到: </label></td>
                    <td><input id="startsignin" type="text" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
                    <td><label for="endsignin">签到结束时间: </label></td>
                    <td><input id="endsignin" type="text" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
					
				</tr>
				<tr>
					<td><label for="dutyendtime">下班时间: </label></td>
                    <td><input id="dutyendtime" type="text" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
					<td><label for="earlyofftime">早退计时: </label></td>
					<td><input id="earlyofftime" type="text" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>		
				    <td><label for="signouttime">签退结束时间: </label></td>
                    <td><input id="signouttime" type="text" onclick="WdatePicker({dateFmt:'H:mm:ss'})"></td>
				</tr>
				<input data-dojo-type="dijit/form/TextBox" type="hidden" data-dojo-props="trim: true" id="_dutystarttime" name="dutystarttime" />
				<input data-dojo-type="dijit/form/TextBox" type="hidden" data-dojo-props="trim: true" id="_startsignin" name="startsignin" />
				<input data-dojo-type="dijit/form/TextBox" type="hidden" data-dojo-props="trim: true" id="_endsignin" name="endsignin" />
				<input data-dojo-type="dijit/form/TextBox" type="hidden" data-dojo-props="trim: true" id="_dutyendtime" name="dutyendtime" />
				<input data-dojo-type="dijit/form/TextBox" type="hidden" data-dojo-props="trim: true" id="_earlyofftime" name="earlyofftime" />
				<input data-dojo-type="dijit/form/TextBox" type="hidden" data-dojo-props="trim: true" id="_signouttime" name="signouttime" />	
			</table>
		</div>
	</form>
	<div style="padding: 5px;">
		<button id="dutysectionInsertBtn" data-dojo-type="dijit/form/Button"
			type="submit" label="新增">
			<script type="dojo/method" event="onClick" args="evt">
        dijit.byId('dutysectionIFD').show();
      </script>
		</button>
		<button id="dutysectionDeleteBtn" data-dojo-type="dijit/form/Button"
			type="button" label="删除" onclick="dutysectionDelete();"></button>
		<button id="dutysectionUpdateBtn" data-dojo-type="dijit/form/Button"
			type="button" label="更新">
			<script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#dutysectionGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var dutysection = jQuery("#dutysectionGird").jqGrid('getRowData', ids[0]);
          dijit.byId('dutysectionUFD').setValues(dutysection);
          dijit.byId('dutysectionUFD').show();
        }
      </script>
		</button>
		<button id="dutysectionQueryBtn" data-dojo-type="dijit/form/Button"
			type="button" label="查询"
			onclick="dutysectionQuery('queryDutySectionForm');"></button>
	</div>
</fieldset>
<div id="gird">
	<table id="dutysectionGird"></table>
	<div id="dutysectionToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#dutysectionGird").jqGrid({
        url : 'dutysection/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 350,
        colNames : [ '班次ID', '班次名称', '开始签到', '上班时间', '签到结束时间', '早退计时', '下班时间', '签退结束' ],
        colModel : [ {
            name : 'sectionid',
            index : 'sectionid',
            width : 60,
            key : true,
            hidden : true,
            align : 'center'
        }, {
            width : 140,
            name : 'sectionname',
            index : 'sectionname'
        }, {
            width : 140,
            name : 'startsignin',
            index : 'startsignin'
        }, {
            width : 140,
            name : 'dutystarttime',
            index : 'dutystarttime'
        }, {
            width : 140,
            name : 'endsignin',
            index : 'endsignin'
        }, {
            width : 140,
            name : 'earlyofftime',
            index : 'earlyofftime'
        }, {
            width : 140,
            name : 'dutyendtime',
            index : 'dutyendtime'
        }, {
            width : 140,
            name : 'signouttime',
            index : 'signouttime'
        } ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#dutysectionToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "班次表列表"
    });
    jQuery("#dutysectionGird").jqGrid('navGrid', '#dutysectionToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
